<?php	if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
 * Tokaban Standard System 
 * Tokaban framework for PHP
 *
 * @package		ap_standard_system.
 * @author		Tokaban R&D Team.
 * @copyright	Copyright (c) 2009, Tokaban, Inc. 
 * @version		Version 4.0
 *
 */  
// ------------------------------------------------------------------------ 

class do_img_view extends Controller 
{
	// Dinh nghia cac bien luon luon phai co
	var $g_nid_session			= '';
	var $g_nid_user				= '';
	var $g_language				= '';

	var $g_form_link			= ''; 
	var $g_form_message			= '';
	var $g_form_title			= '';
	var $g_hidden_button		= '0';
	
	var $g_current_page			= '';	// Trang hien hanh can hien thi.
	var $g_row_count			= '';	// tong so mau tin tim thay.
	var $g_row_per_page			= '';	// tong so mau tin tren trang.
	var $g_page_count			= '';	// tong so mau tin tren trang.
	
	var $g_sort					= '';	// incon sort.
	var $g_field_sort			= '';	// ten field dung chiu tac dong sort thong tin.
	
	var $g_query_string			= '';	// Cau truy van cua doi tuong can hien thi.
	var $g_where_string			= '';	// Menh de where cua cau truy van
	var $g_order_string			= '';	// Menh de order by cua cau truy van

	var $g_obj_data				= '';
	var $g_obj_row_per_page		= '';
	var $g_language_key			= 'img_view';
	var $f_path					= '.././upload/';
	
	var $cimg_name				= '';
	var $cimg_name_encrypt		= '';
	var $cindex					= '';
	var $count_img					= '';	
	

// ------------------------------------------------------------------------
// 	TOKABAN STANDARD PHP CODE
//  CONSTRUCTOR CLASS
function do_img_view()
	{
		parent::Controller();
		session_start();
		$this->load->database();
		$this->load->helper('ap_db');	
		$this->load->helper('ap_function');
		$this->load->helper('ap_html');
		$this->load->helper('ap_view');
		$this->load->helper('ap_object');
		
		$this->tokaban_system_check = '1';
		// LOAD CAC THU VIEN KHAC NEU CO.
	}

// 	TOKABAN STANDARD PHP CODE	
//	CHI MUC CO BAN CUA MOT DOI TUONG CLASS
function index()
	{	
		$this->do_process();		
	}		
	
// 	TOKABAN STANDARD PHP CODE	
//  XU LY TUAN TU CAC 4 BUOC CO BAN CUA MOT TRANG
private function do_process()
	{
		$this->get_data();
		$this->caculate_data();
		$this->do_business();
		$this->destroy_data();
	}
	
// 	TOKABAN STANDARD PHP CODE		
//  NHAN TAT CA CAC GIA TRI DAU VAO CO THE NHAN DUOC
private function get_data()
	{
		
		// Xac dinh cac bien he thong.
		$this->g_nid_session		= Fget_userdata('gs_nid_session');
		$this->g_nid_user			= Fget_userdata('gs_nid_user');
		$this->g_language			= Fget_userdata('gs_language');
		
		$this->g_form_message		= $this->get_cookie('g_form_message');
		$this->set_cookie('g_form_message','');
		
		// Xac dinh gia tri cac truong tim kiem
		
		if (isset($_POST['g_hidden_form_event']))
			$this->g_hidden_form_event 		= $_POST['g_hidden_form_event'];
		else
			$this->g_hidden_form_event 		= $this->get_cookie('g_hidden_form_event');
			
		if(isset($_POST['g_hidden_nid']))
			$this->g_hidden_nid 		= $_POST['g_hidden_nid'];
		else			
			$this->g_hidden_nid 		= $this->get_cookie('g_hidden_nid');

		if (isset($_POST['g_hidden_button']))
		{
			$this->g_hidden_button 			= $_POST['g_hidden_button'];
		}
		// Xac dinh cac gia tri hien thi trang.
		$this->g_row_per_page		= Fget_userdata('gs_row_per_page');
		$this->g_current_page		= $this->get_cookie('g_current_page');
		$this->g_sort				= $this->get_cookie('g_sort');
		$this->g_field_sort			= $this->get_cookie('g_field_sort');
	}

// 	TOKABAN STANDARD PHP CODE
//  XU LY CAC BIEN HOP LE TRUOC KHI KIEM TRA O BUOC SAU
private function caculate_data()
	{
		$this->load->language('ap', 'vni');
	//	$this->load->language($this->g_language_key, $this->g_language);		
		
		$this->g_form_link 	= base_url().'index.php/do_img_view';
		//$this->g_form_title = $this->lang->line('do_img_view');
		
		if($this->g_sort=='')
			{
				$this->g_sort='asc';
				$this->set_cookie('g_sort',$this->g_sort );
			}
		if($this->g_field_sort=='')
			{
				$this->g_field_sort='nid';
				$this->set_cookie('g_field_sort',$this->g_field_sort );
			}

		$this->g_query_string 		= Vget_img_view();
		$this->g_where_string		= $this->get_where_string();
		$this->g_order_string		= $this->get_order_string();
		
		$this->g_row_count			= $this->get_count_listview();
		$this->g_page_count			= Fget_total_page($this->g_row_per_page,$this->g_row_count);

		if($this->g_row_per_page=='' OR $this->g_row_per_page<=0)
			$this->g_row_per_page 	= Fget_userdata('gs_row_per_page');
		if($this->g_row_per_page=='' OR $this->g_row_per_page<=0)
			$this->g_row_per_page 	= 1;

		if($this->g_current_page=='' OR $this->g_current_page<=0)
			$this->g_current_page 	= 1;
		if($this->g_current_page>$this->g_page_count)
			$this->g_current_page 	= $this->g_page_count;		
		if($this->g_row_count<=0)
			$this->g_current_page 	= 0;			
		$this->set_cookie('g_current_page',$this->g_current_page );
				
		$this->g_obj_data			= $this->get_data_listview();
		$this->g_obj_row_per_page	= Obj_get_rows_select_list();
		
		// Xac dinh thong tin can thiet de hien thi tren listview
		if($this->g_sort=='asc')
			$this->g_sort='desc';	
		else
			$this->g_sort='asc';
		
		if($this->g_hidden_button ==='del_img')
		{
			if($this->del_img() ==TRUE)
				redirect('do_img_view');
			else
				redirect('user.dashboard');		
		}		
	}

// 	TOKABAN STANDARD PHP CODE
//  XU LY DU LIEU, DIEU HUONG LUONG XU LY THONG TIN
private function do_business()
	{
		$data['g_form_link']			= $this->g_form_link;
		$data['g_form_message']			= $this->g_form_message;
		
		$data['g_hidden_button']			= $this->g_hidden_button;

		$data['g_form_title']			= $this->g_form_title;		
		$data['lbl_form_title'] 		= 'Thư viện ảnh';
		$data['lbl_print_report']		= $this->lang->line('lbl.0000.print_report');
		$data['lbl_export_excel']		= $this->lang->line('lbl.0000.export_excel');
		$data['lbl_advance_search']		= $this->lang->line('lbl.0000.add_new_record');
		$data['lbl_select']				= $this->lang->line('lbl.0000.select');
		$data['lbl_view_detail']		= $this->lang->line('lbl.0000.view_detail');
				
		if($this->g_current_page<$this->g_page_count)
			$data['lbl_next']			= $this->lang->line('lbl.0000.next');
		else
			$data['lbl_next']			= '';
		if($this->g_current_page>1 AND $this->g_current_page<=$this->g_page_count)	
			$data['lbl_previous']		= $this->lang->line('lbl.0000.previous');
		else
			$data['lbl_previous']		= '';
			
		$data['lbl_page']				= $this->lang->line('lbl.0000.page');
		$data['lbl_row_per_page']		= $this->lang->line('lbl.0000.row_per_page');
		$data['lbl_total_row']			= $this->lang->line('lbl.0000.total_row');
		$data['lbl_print']				= $this->lang->line('lbl.0000.print_report');
		$data['lbl_export']				= $this->lang->line('lbl.0000.export_excel');
		$data['lbl_advance_search']		= $this->lang->line('lbl.0000.advance_search');
		
		$data['lbl_add_new']			= $this->lang->line('lbl.0000.add_new');
		$data['lbl_delete']				= $this->lang->line('lbl.0000.delete');
		
		$data['g_current_page']			= $this->g_current_page;
		$data['g_row_count']			= $this->count_img;
		$data['g_page_count']			= $this->g_page_count;
		$data['g_row_per_page']			= $this->g_row_per_page;
		$data['menu_active']			= 'img_view';
		$data['cimg_name']					= $this->get_language('cimg_name');
		$data['cimg_name_encrypt']		 			= $this->get_language('cimg_name_encrypt');
		$data['cindex']				= $this->get_language('cindex');
		$data['cenddate']				= $this->get_language('cenddate');
		$data['cstatus']				= $this->get_language('cstatus');
				
		$data['gencbo_g_row_per_page']	= Fgen_html_combobox('no', 'g_row_per_page', $this->g_row_per_page,'50',$this->g_obj_row_per_page, 'cvalue', $this->g_language,'nosubmit');
						
		$data['g_obj_data']				= $this->g_obj_data;
		
		$data['g_sort']					= $this->g_sort;
		$data['g_field_sort']			= $this->g_field_sort;			
		$this->load->view('img_view_listview/index',$data);
	}

// 	TOKABAN STANDARD PHP CODE	
// 	HUY CAC BIEN DOI TUONG NEU CAN THIET TAI DAY.
private function destroy_data()
	{
	}
// 	TOKABAN STANDARD PHP CODE
//	Nhan cac tham so dau vao cua doi tuong neu co
private function get_language($p_key)
	{
		$obj_key='lbl.img_view.';
		return $this->lang->line($obj_key.$p_key);
	}
	

	
// 	TOKABAN STANDARD PHP CODE
//	Nhan cac tham so dau vao cua doi tuong neu co
private function get_cookie($p_key)
	{
		$key=Oget_img_view_key().$p_key;
		return dbget_cookie($key);
	}
		
// 	TOKABAN STANDARD PHP CODE	
//	Nhan cac tham so dau vao cua doi tuong neu co
private function set_cookie($p_key, $p_value)
	{
		$key=Oget_img_view_key().$p_key;
		dbset_cookie($key,$p_value);
	}

// 	TOKABAN STANDARD PHP CODE	
//	Nhan cac tham so dau vao cua doi tuong neu co.
private function get_where_string()
	{		
		$str_resturn='';		
		return $str_resturn;
	}

// 	TOKABAN STANDARD PHP CODE	
//	Nhan cac tham so dau vao cua doi tuong neu co.
private function get_order_string()
	{
		return ' order by  cindex asc';
	}

// 	TOKABAN STANDARD PHP CODE	
//	Xac dinh bo gia tri can hien thi tren man hinh listview.
// 	Truoc khi thuc hien ham nay, can phai chuan bi tat ca cac bien can thiet co lien quan den ket qua truy van	
private function get_data_listview()
	{
		$str_query = $this->g_query_string;
		$str_query .= ' '.$this->g_where_string . ' ';
					
		if($this->g_order_string!='')
			$str_query .= ' '.$this->g_order_string . ' ';
		
		if($this->g_row_count>0)
			$str_query .= ' limit '. ($this->g_current_page -1 )* $this->g_row_per_page*4 . ' , ' . $this->g_row_per_page*4;		

		$obj_result = $this->db->query($str_query);  		
		return $obj_result->result_array();
	}
// 	TOKABAN STANDARD PHP CODE	
//	Xac dinh bo gia tri can hien thi tren man hinh listview.
// 	Truoc khi thuc hien ham nay, can phai chuan bi tat ca cac bien can thiet co lien quan den ket qua truy van
private function get_count_listview()
	{
		$str_query = $this->g_query_string;
		$str_query .= ' '.$this->g_where_string . ' ';
		$obj_result = $this->db->query($str_query);
		$this->count_img	=$obj_result->num_rows();
		return round($obj_result->num_rows()/4 +0.1);
	}
private function del_img()
{
	if (!empty($_POST['img_check']))
	{	
		foreach ($_POST['img_check'] as $nid)
		{
			//del file		
			$this->db->where('nid',$nid);
			$obj_result	=$this->db->get('timg_store');			
			if($obj_result->num_rows>0)
			{
				$obj_result	=$obj_result->row_array();
				@unlink($this->f_path.$obj_result['cimg_name_encrypt']);
			}
		
			$this->db->where('nid', $nid);
			$this->db->delete('timg_store'); 
		}
		return TRUE;
	}
}
	
//
// DINH NGHIA CAC HAM LOGIC CAN THIET KHAC TAI DAY
//
	
// END do_init class
}

/* End of file do_login.php */
/* Location: controller/do_login.php */